﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Controls;

namespace PPI
{
    public partial class frmBase : DevComponents.DotNetBar.Office2007Form
    {
        public frmBase()
        {
            InitializeComponent();

            // setup load background worker

            AllowCloseDuringLoad = true;
            m_BackgroundWorkerLoad.DoWork += new DoWorkEventHandler(LoadDoWork);
            m_BackgroundWorkerLoad.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LoadCompleted);

            // setup save background worker

            AllowCloseDuringSave = false;
            m_BackgroundWorkerSave.DoWork += new DoWorkEventHandler(SaveDoWork);
            m_BackgroundWorkerSave.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SaveCompleted);
        }

        protected virtual void Form_Load(object sender, EventArgs e)
        {
            // do nothing
        }

        protected virtual void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            // test save background worker
            
            if (m_BackgroundWorkerSave.IsBusy && !AllowCloseDuringSave)
            {
                e.Cancel = true;
                return;
            }

            // test load background worker

            if (m_BackgroundWorkerLoad.IsBusy && !AllowCloseDuringLoad)
            {
                e.Cancel = true;
                return;
            }
        }

        #region Save

        /// <summary>
        /// If a background save is in process, should this form be allowed to close?
        /// </summary>
        protected bool AllowCloseDuringSave
        {
            get;
            set;
        }

        protected BackgroundWorker m_BackgroundWorkerSave = new BackgroundWorker();

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            SaveStart();
        }

        protected virtual void SaveStart()
        {
            m_BackgroundWorkerSave.RunWorkerAsync();
        }

        protected virtual void SaveDoWork(object sender, DoWorkEventArgs e)
        {
            // nothing to do
        }

        protected virtual void SaveCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // nothing to do
        }

        #endregion

        #region Load

        /// <summary>
        /// If a background load is in process, should this form be allowed to close?
        /// </summary>
        protected bool AllowCloseDuringLoad
        {
            get;
            set;
        }

        protected BackgroundWorker m_BackgroundWorkerLoad = new BackgroundWorker();

        protected virtual void LoadStart()
        {
            m_BackgroundWorkerLoad.RunWorkerAsync();
        }

        protected virtual void LoadDoWork(object sender, DoWorkEventArgs e)
        {
            // nothing to do
        }

        protected virtual void LoadCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // nothing to do
        }

        #endregion

        public enum FormSizes
        {
            Small,
            Medium,
            Large,
            Smaller
        }

        private FormSizes m_FormSize;
        [Browsable(true)]
        [Category("Layout")]
        [DefaultValue(FormSizes.Large)]
        public FormSizes FormSize
        {
            set
            {
                m_FormSize = value;
                switch (m_FormSize)
                {
                    case FormSizes.Smaller:
                        this.Size = new Size(400, 150);
                        break;
                    case FormSizes.Small:
                        this.Size = new Size(400, 300);
                        break;
                    case FormSizes.Medium:
                        this.Size = new Size(640, 480);
                        break;
                    case FormSizes.Large:
                        this.Size = new Size(800, 600);
                        break;
                }
            }
            get
            {
                return m_FormSize;
            }
        }


        public void SetTitle(string t)
        {
            this.TitleText = t;
            this.Text = t;
        }

        #region Validators

        public List<Validator> GetAllValidators()
        {
            return GetAllValidators(this);
        }
        public List<Validator> GetAllValidators(Control parent)
        {
            List<Validator> _Validators = new List<Validator>();
            foreach (Control _Control in parent.Controls)
                if (_Control is Validator)
                    _Validators.Add((Validator)_Control);
                else if (_Control.Controls.Count > 0)
                    _Validators.AddRange(GetAllValidators(_Control));
            return _Validators;
        }
        public bool ValidateAll()
        {
            bool _IsValid = true;
            foreach (Validator _Val in GetAllValidators())
            {
                _Val.Validate();
                _IsValid &= _Val.IsValid;
            }
            return _IsValid;
        }


        #endregion

    }
}
